package util;

import java.security.MessageDigest;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author lucifer
 */
public class InputHelper {

  public static final String SQLForeignKey = "23503";

  public static final String PanicMessage = "No se puede acceder a la base de datos. Por favor, verifique la conexión y configuración de la base de datos.";

  public static boolean isInteger(String name) {
    try {
      Integer.parseInt(name);
    } catch (Exception e) {
        return false;
    }
    return true;
    
  }

  public static boolean isDouble(String name) {
     try {
      Double.parseDouble(name);
    } catch (Exception e) {
        return false;
    }
    return true;
  }

  public static boolean isNonAlphanumeric(String name) {
    for (int i = 0; i < name.length(); i++) {
      if (!Character.isLetterOrDigit(name.charAt(i)) && name.charAt(i) != ' ' && name.charAt(i) != '-' && name.charAt(i) != ',' && name.charAt(i) != '.') {
        return true;
      }
    }
    String empty = name.replace(" ", "");
    return empty.equals("");
  }

  public static boolean isNonAlphabetic(String name) {
    for (int i = 0; i < name.length(); i++) {
      if (!Character.isAlphabetic(name.charAt(i)) && name.charAt(i) != ' ') {
        return true;
      }
    }
    String empty = name.replace(" ", "");
    return empty.equals("");
  }

  public static boolean isNonValidPhone(String phone) {
    for (int i = 0; i < phone.length(); i++) {
      if (!Character.isDigit(phone.charAt(i)) && phone.charAt(i) != ' ' & phone.charAt(i) != '-') {
        return true;
      }
    }
    return false;
  }

  public static boolean isValidEmail(String email) {
    String patternEmail = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
    Pattern pattern = Pattern.compile(patternEmail);
    Matcher matcher = pattern.matcher(email);
    return matcher.matches();
  }

  @SuppressWarnings("deprecation")
  public static LocalDateTime FromOldDateToNew(Date d) {
    return LocalDateTime.of(d.getYear() + 1900, d.getMonth() + 1, d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds());
  }

  @SuppressWarnings("deprecation")
  public static Date FromNewDateToOld(LocalDateTime d) {
    return new Date(d.getYear() - 1900, d.getMonthValue() - 1, d.getDayOfMonth(), d.getHour(), d.getMinute(), d.getSecond());
  }

  // OKAY
  public static String DayConverter(DayOfWeek day) {
    String d = null;
    switch (day) {
    case MONDAY:
      d = "Lunes";
      break;
    case TUESDAY:
      d = "Martes";
      break;
    case WEDNESDAY:
      d = "Miércoles";
      break;
    case THURSDAY:
      d = "Jueves";
      break;
    case FRIDAY:
      d = "Viernes";
      break;
    case SATURDAY:
      d = "Sábado";
      break;
    case SUNDAY:
      d = "Domingo";
      break;
    }
    return d;
  }

  // OKAY
  public static String SHA512(String password) {
    MessageDigest digest = null;
    byte[] hash = null;

    try {
      digest = MessageDigest.getInstance("SHA-512");
      hash = digest.digest(password.getBytes("UTF-8"));
    } catch (Exception e) {
      // I hate bloody silly non useful exceptions
    }

    String s = "";
    for (int i = 0; i < hash.length; i++) {
      String hex = Integer.toHexString(0xff & hash[i]);
      if (hex.length() == 1) {
        s += '0';
      }
      s += hex;
    }

    return s;
  }

}
